
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// Pay_PaymentTypeDAL Data Class
    /// </summary>
    public partial class Pay_PaymentTypeDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Pay_PaymentType";

        public Pay_PaymentTypeDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Pay_PaymentType model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [Pay_PaymentType](");
            strSql.Append("[PaymentTypeID],[Name],[Getway],[SortNo],[Desc],[Settings],[MerchantNo],[MerchantName],[MerchantKey],[Status]");
            strSql.Append(") values (");
            strSql.Append("@PaymentTypeID,@Name,@Getway,@SortNo,@Desc,@Settings,@MerchantNo,@MerchantName,@MerchantKey,@Status");
            strSql.Append(")");

            SqlParameter[] parameters = {
                    new SqlParameter("@PaymentTypeID", GetMaxID()+1),
                    new SqlParameter("@Name", model.Name),
                    new SqlParameter("@Getway", model.Getway),
                    new SqlParameter("@SortNo", model.SortNo),
                    new SqlParameter("@Desc", model.Desc),
                    new SqlParameter("@Settings", model.Settings),
                    new SqlParameter("@MerchantNo", model.MerchantNo),
                    new SqlParameter("@MerchantName", model.MerchantName),
                    new SqlParameter("@MerchantKey", model.MerchantKey),
                    new SqlParameter("@Status",model.Status)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Pay_PaymentType model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [Pay_PaymentType] set ");
            strSql.Append("[Name]=@Name,[Getway]=@Getway,[SortNo]=@SortNo,[Desc]=@Desc,[Settings]=@Settings,[MerchantNo]=@MerchantNo,[MerchantName]=@MerchantName,[MerchantKey]=@MerchantKey,[Status]=@Status");
            strSql.Append(" where [PaymentTypeID]=@PaymentTypeID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@PaymentTypeID", model.PaymentTypeID),
                    new SqlParameter("@Name", model.Name),
                    new SqlParameter("@Getway", model.Getway),
                    new SqlParameter("@SortNo", model.SortNo),
                    new SqlParameter("@Desc", model.Desc),
                    new SqlParameter("@Settings", model.Settings),
                    new SqlParameter("@MerchantNo", model.MerchantNo),
                    new SqlParameter("@MerchantName", model.MerchantName),
                    new SqlParameter("@MerchantKey", model.MerchantKey),
                    new SqlParameter("@Status",model.Status)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(String PaymentTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Pay_PaymentType] ");
            strSql.Append(" where [PaymentTypeID]=@PaymentTypeID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@PaymentTypeID", PaymentTypeID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxID()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select max(PaymentTypeID) from [Youz.Shop].[dbo].[Pay_PaymentType]");

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Pay_PaymentType Get(String PaymentTypeID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [Pay_PaymentType] ");
            strSql.Append(" where [PaymentTypeID]=@PaymentTypeID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("PaymentTypeID", PaymentTypeID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Pay_PaymentType Get(DataRow row)
        {
            if (row == null) return null;

            Pay_PaymentType model = new Pay_PaymentType();
	
            
            if (row["PaymentTypeID"] != DBNull.Value)
            {
                model.PaymentTypeID = (String)row["PaymentTypeID"];
            }      
            
            if (row["Name"] != DBNull.Value)
            {
                model.Name = (String)row["Name"];
            }      
            
            if (row["Getway"] != DBNull.Value)
            {
                model.Getway = (String)row["Getway"];
            }      
            
            if (row["SortNo"] != DBNull.Value)
            {
                model.SortNo = (Int32)row["SortNo"];
            }      
            
            if (row["Desc"] != DBNull.Value)
            {
                model.Desc = (String)row["Desc"];
            }      
            
            if (row["Settings"] != DBNull.Value)
            {
                model.Settings = (String)row["Settings"];
            }      
            
            if (row["MerchantNo"] != DBNull.Value)
            {
                model.MerchantNo = (String)row["MerchantNo"];
            }      
            
            if (row["MerchantName"] != DBNull.Value)
            {
                model.MerchantName = (String)row["MerchantName"];
            }      
            
            if (row["MerchantKey"] != DBNull.Value)
            {
                model.MerchantKey = (String)row["MerchantKey"];
            }
            if (row["Status"] != DBNull.Value)
            {
                model.Status = (bool)row["Status"];
            }

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [Pay_PaymentType] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Pay_PaymentType] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Pay_PaymentType] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Pay_PaymentType]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by PaymentTypeID desc ", out count, cmdParms);
            total = count;
            return dt;
        }

        public List<Pay_PaymentType> GetList(string LanguageKey)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select PaymentTypeID,isnull((select LanguageValue from Lang_DataLanguage where TableName='Pay_PaymentType' and ColumnName='Name' and ObjectID=Pay_PaymentType.Name and LanguageCode=@LanguageCode),Pay_PaymentType.Name) as Name,Getway,SortNo,[Desc],Settings,MerchantNo,MerchantName,MerchantKey,Status from Pay_PaymentType ");
            sb.Append(" where Status=1 order by SortNo desc");
            SqlParameter[] Parameter = 
            {
                new SqlParameter("@LanguageCode",LanguageKey)
            };
            DataSet ds = DbHelperSQL.Query(sb.ToString(), Parameter);
            List<Pay_PaymentType> list = new List<Pay_PaymentType>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(Get(row));
                }
            }
            return list;
        }
      #endregion  成员方法
    }
}


  